﻿/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using Daidalos.Patterns;
using Daidalos.Components;
using Daidalos.Exception;
using Daidalos.Graphics;

namespace Daidalos.Management
{
    public sealed class UIManager : SingletonManager<UIManager>
    {

        #region Attributes
        /// <summary>
        ///     A reference to the focussed component.
        /// </summary>
        private Component focussedComponent = null;

        /// <summary>
        ///    A reference to the hovered component.
        /// </summary>
        private Component hoveredComponent = null;

        /// <summary>
        ///     The hidden panel serves as a fullscreen
        ///     anchor element.
        /// </summary>
        /// <remarks>
        ///     Thus the panel serves as the root node
        ///     in the UI hierarchy.
        /// </remarks>
        private Panel anchorPanel = new Panel("__anchor_panel", 0, 0, RenderDevice.Instance.Window.Width, RenderDevice.Instance.Window.Height);

        /// <summary>
        ///     Stores a reference to the object currently
        ///     hit by a user action.
        /// </summary>
        private Component currentHit = null;
        #endregion

        #region Properties
        public Component CurrentHit
        {
            get
            {
                return currentHit;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        ///     Adds a component to the UI.
        /// </summary>
        /// <param name="label">
        ///     The label for the component.
        /// </param>
        /// <param name="widget">
        ///     The component itself.
        /// </param>
        public void AddComponent(Component widget)
        {
            // Delegate the invocation to the anchor element.
            anchorPanel.Add(widget);
        }

        public Component OnMouseAction(int button, float left, float top)
        {
            focussedComponent = anchorPanel.GetTargetedComponent(left, top);
            return focussedComponent;
        }

        /// <summary>
        ///    Display the user interface in its current state.
        /// </summary>
        public void Display()
        {
            // Verify state.
            if (anchorPanel == null)
            {
                throw new InvalidStateException("The anchor panel was lost, resulting in a corrupted UI state!");
            }

            // Delegate the invocation to the anchor element.
            anchorPanel.Display();
        }
        #endregion

    }
}
